package cn.hsa.fqz.task.service.impl;

import cn.hsa.fqz.task.dao.TTaskMedcClueFeedbackMapper;
import cn.hsa.fqz.task.dao.TTaskMedcClueOffsiteMapper;
import cn.hsa.fqz.task.entity.RegionDO;
import cn.hsa.fqz.task.entity.TTaskMedcClueOffsiteDO;
import cn.hsa.fqz.task.service.RegionService;
import cn.hsa.fqz.task.service.TTaskMedcClueOffsiteService;
import cn.hsa.fqz.task.service.dto.TTaskMedcClueOffsiteDTO;
import cn.hsa.fqz.task.service.dto.TaskQueryDTO;
import cn.hsa.hsaf.core.framework.util.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author Administrator
 * @version 1.0
 * @date 2023-09-22 16:36
 */
@Service
public class TTaskMedcClueOffsiteServiceImpl implements TTaskMedcClueOffsiteService {

    @Autowired
    private TTaskMedcClueOffsiteMapper tTaskMedcClueOffsiteMapper;

    @Autowired
    private TTaskMedcClueFeedbackMapper tTaskMedcClueFeedbackMapper;

    @Autowired
    private RegionService regionService;

    @Override
    public PageResult<TTaskMedcClueOffsiteDTO> getPageByCondition(TaskQueryDTO query) {
        if (query.getCondition() == null) {
            return null;
        }
        Page<TTaskMedcClueOffsiteDO> page =  PageHelper.startPage(query.getPage().getPageNum(), query.getPage().getPageSize());
        List<TTaskMedcClueOffsiteDO> list = tTaskMedcClueOffsiteMapper.selectOffsitePageByCondition(query);
        List<TTaskMedcClueOffsiteDTO> dtos = fixTTaskMedcClueOffsiteDTOS(list);
        PageResult<TTaskMedcClueOffsiteDTO> result = new PageResult<>();
        result.setData(dtos);
        result.setPageNum(page.getPageNum());
        result.setPageSize(page.getPageSize());
        result.setPages(page.getPages());
        result.setRecordCounts((int)page.getTotal());
        return result;
    }

    private List<TTaskMedcClueOffsiteDTO> fixTTaskMedcClueOffsiteDTOS(List<TTaskMedcClueOffsiteDO> list) {
        List<TTaskMedcClueOffsiteDTO> result = Lists.newArrayList();
        List<RegionDO> regionDOS = regionService.getRegionsBypRegion("999");
        List<RegionDO> cityDOS = regionService.getAllCitys();
        for (TTaskMedcClueOffsiteDO offsite:list) {
            TTaskMedcClueOffsiteDTO dto = new TTaskMedcClueOffsiteDTO();
            BeanUtils.copyProperties(offsite,dto);
            Integer count = tTaskMedcClueFeedbackMapper.selectCountByTaskCodeAndMedcCode(offsite.getTaskCode(), offsite.getMedcCode());
            dto.setCountNum(count==null?0:count);
            Optional<RegionDO> poptional = regionDOS.stream().filter(re -> offsite.getProviceCode().equals(String.valueOf(re.getRegion()))).findFirst();
            if(poptional.isPresent()){
                RegionDO fliterData = poptional.get();
                dto.setProviceName(fliterData.getProvice());
            }
            Optional<RegionDO> coptional = cityDOS.stream().filter(res -> offsite.getCityCode().equals(String.valueOf(res.getRegion()))).findFirst();
            if(coptional.isPresent()){
                RegionDO fliterData = coptional.get();
                dto.setCityName(fliterData.getCity());
            }
            result.add(dto);
        }
        return result;
    }

    @Override
    public Integer addClueOffsite(TTaskMedcClueOffsiteDTO dto) {
        TTaskMedcClueOffsiteDO offsiteDO = new TTaskMedcClueOffsiteDO();
        BeanUtils.copyProperties(dto,offsiteDO);
        offsiteDO.setStatus(1);
        return tTaskMedcClueOffsiteMapper.insertSelective(offsiteDO);
    }

    @Override
    public Boolean isExsitsOffsite(Integer taskId, String medcCode,String cityCode,String uid) {
        Integer result = tTaskMedcClueOffsiteMapper.countByTaskIdAndMedcCodeAndCityCode(taskId, medcCode,cityCode,uid);
        return result.intValue()>0?true:false;
    }

    @Override
    public Integer deleteClueOffsite(Integer offsiteId) {
        return tTaskMedcClueOffsiteMapper.deleteClueOffsite(offsiteId);
    }
}
